﻿using Common.Utils;
using Common.Utils.Helper;
using Game.Kernel;
using Game.Logic.Data;
using Game.Logic.Events;
using Game.Logic.Manager;
using TMPro;
using UIFramework;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Game.Logic.Login
{
    public class LoginView : GameUIView
    {
        public LoginController LoginController { private set; get; }
        public InputField InputLoginAccount { get => m_inputLoginAccount; }
        public InputField InputLoginPassword { get => m_inputLoginPassword; }
        public bool IsRegister { private set; get; }

        private InputField m_inputLoginAccount;
        private InputField m_inputLoginPassword;
        private Button m_btnRegist;
        private Button m_btnSwitch;
        private Button m_btnLogin;
        private Button m_btnLogout;
        private Button m_btnCancel;
        private Text m_textCurAccount;

        void Awake()
        {
            RegisterGameUIEvent();
            m_inputLoginAccount = UnityHelper.GetTheChildComponent<InputField>(gameObject, "Input_Account");
            m_inputLoginPassword = UnityHelper.GetTheChildComponent<InputField>(gameObject, "Input_Password");
            m_btnCancel = UnityHelper.GetTheChildComponent<Button>(gameObject, "Btn_Cancel");
            m_btnLogout = UnityHelper.GetTheChildComponent<Button>(gameObject, "Btn_Logout");
            m_btnRegist = UnityHelper.GetTheChildComponent<Button>(gameObject, "Btn_Regist");
            m_btnSwitch = UnityHelper.GetTheChildComponent<Button>(gameObject, "Btn_Switch");
            m_btnLogin = UnityHelper.GetTheChildComponent<Button>(gameObject, "Btn_Login");
            m_textCurAccount = UnityHelper.GetTheChildComponent<Text>(gameObject, "Txt_CurAccount");

            Reset();
        }

        protected override void RegisterGameUIEvent()
        {
            GameUIEnterEvent = GameUIEvent.Create(GameEventType.GameUIEnter, ENUM_UITYPE.LOGIN);
        }

        private void Reset()
        {
            m_inputLoginAccount.gameObject.SetActive(false);
            m_inputLoginPassword.gameObject.SetActive(false);
            m_btnRegist.gameObject.SetActive(false);
            m_btnSwitch.gameObject.SetActive(false);
            m_btnLogout.gameObject.SetActive(false);
            m_btnCancel.gameObject.SetActive(false);
            m_textCurAccount.gameObject.SetActive(false);
        }

        // Use this for initialization
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            
        }

        public override void OnEnter(params object[] _params)
        {
            GameEventManager.Instance.DispatchEvent(GameUIEnterEvent);

            LoginController = Controller as LoginController;
            EventTriggerListener.Get(m_btnLogin.gameObject).SetEventHandle(ENUM_UIEVENT_TYPE.ONCLICK, LoginController.OnClickLogin);
            EventTriggerListener.Get(m_btnLogout.gameObject).SetEventHandle(ENUM_UIEVENT_TYPE.ONCLICK, LoginController.OnClickLogout);

            EventTriggerListener.Get(m_btnRegist.gameObject).SetEventHandle(ENUM_UIEVENT_TYPE.ONCLICK, OnClickRegist);
            EventTriggerListener.Get(m_btnSwitch.gameObject).SetEventHandle(ENUM_UIEVENT_TYPE.ONCLICK, OnClickSwitch);
            EventTriggerListener.Get(m_btnCancel.gameObject).SetEventHandle(ENUM_UIEVENT_TYPE.ONCLICK, OnClickCancel);
        }

        public void OnClickRegist(GameObject go, BaseEventData eventData)
        {
            IsRegister = true;
            m_btnRegist.gameObject.SetActive(false);
            m_btnSwitch.gameObject.SetActive(false);
            m_btnCancel.gameObject.SetActive(true);
            m_inputLoginAccount.gameObject.SetActive(true);
            m_inputLoginPassword.gameObject.SetActive(true);
        }

        public void OnClickSwitch(GameObject go, BaseEventData eventData)
        {
            IsRegister = false;
            m_btnRegist.gameObject.SetActive(false);
            m_btnSwitch.gameObject.SetActive(false);
            m_btnCancel.gameObject.SetActive(true);
            m_inputLoginAccount.gameObject.SetActive(true);
            m_inputLoginPassword.gameObject.SetActive(true);
        }

        public void OnClickCancel(GameObject go, BaseEventData eventData)
        {
            m_btnRegist.gameObject.SetActive(true);
            m_btnSwitch.gameObject.SetActive(true);
            m_btnCancel.gameObject.SetActive(false);
            m_inputLoginAccount.text = string.Empty;
            m_inputLoginPassword.text = string.Empty;
            m_inputLoginAccount.gameObject.SetActive(false);
            m_inputLoginPassword.gameObject.SetActive(false);
        }

        private void RefreshLoginButtonStatus(bool enable)
        {
            m_btnLogin.interactable = enable;
        }

        public override void OnExit()
        {

        }

        public override void OnPause()
        {

        }

        public override void OnResume()
        {

        }

        private void OnRecvGameUserData(GameBaseModelEvent _gme)
        {
            GameUserData gameUser = _gme.Data.Cast<GameUserData>();
            string account = gameUser.UserConfig.GetField("Account");
            string password = gameUser.UserConfig.GetField("Password");

            if (null != account && null != password)
            {
                m_btnRegist.gameObject.SetActive(false);
                m_btnSwitch.gameObject.SetActive(false);
                m_btnLogout.gameObject.SetActive(true);
                m_textCurAccount.gameObject.SetActive(true);
                m_textCurAccount.text = account;
            }
            else
            {
                m_btnRegist.gameObject.SetActive(true);
                m_btnSwitch.gameObject.SetActive(true);
                m_btnLogout.gameObject.SetActive(false);
                m_textCurAccount.gameObject.SetActive(false);
            }
        }

        public override void OnModelEventHandler(GameBaseModelEvent _event)
        {
            GameModelEvent gme = _event.Cast<GameModelEvent>();
            switch (gme.DataType)
            {
                case GameDataType.GameUser:
                    OnRecvGameUserData(gme);
                    break;
                default:
                    break;
            }

            object[] _params = gme.Params;
            RefreshLoginButtonStatus((bool)_params[0]);
        }
    }
}
